﻿Imports Cls_Common
Imports VSModel
Imports System.Data

Partial Class FrontEnd_Control_Oto_List
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            'If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
            '    ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "abc", "ajaxJquery()", True)
            'Else
            '    Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "abc", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            'End If

            LoadTinhThanh()
            TinhThanhChinh()
            BindToGrid(1, 0)



        End If
    End Sub

    Protected Sub LoadTinhThanh()
        Using Data As New VSEntities

            Dim p = (From q In Data.KhuVucs Select q).ToList
            For Each a In p
                'insert khu vuc
                Dim itm As New ListItem
                itm.Text = a.Name
                itm.Value = 0
                itm.Attributes.Add("disabled", "disabled")
                itm.Attributes.Add("class", "Khuvuc")
                ddlTinhThanh.Items.Add(itm)

                Dim _khuvucId = a.Id
                Dim k = (From q In Data.DonViHanhChinhs Where q.KhuVucId = _khuvucId Order By q.Title Ascending Select q).ToList
                For Each item In k
                    Dim itm1 As New ListItem
                    itm1.Value = item.Id
                    itm1.Text = "---" & item.Title
                    itm1.Enabled = True
                    ddlTinhThanh.Items.Add(itm1)
                Next
            Next
        End Using
    End Sub

    Protected Sub TinhThanhChinh()
        Dim curTinhThanh As New DataTable
        curTinhThanh.Columns.Add("Id")
        curTinhThanh.Columns.Add("Title")

        Using Data As New VSEntities

      

            'toan quoc here
            Dim _rowToanQuoc = curTinhThanh.NewRow
            _rowToanQuoc(0) = 0

            _rowToanQuoc(1) = "Toàn quốc"
            curTinhThanh.Rows.Add(_rowToanQuoc)


            'main tinh thanh here
            Dim p = (From q In Data.DonViHanhChinhs Where q.MainTinhThanh = True Select New With {.Id = q.Id, .Title = q.Title}).ToList

            For Each a In p
                Dim _row1 = curTinhThanh.NewRow
                _row1(0) = a.Id
                _row1(1) = a.Title
                curTinhThanh.Rows.Add(_row1)
            Next

            'Dim selectedTinhThanhId As String = ddlTinhThanh.SelectedValue
            ''current choice tinh thanh
            'If selectedTinhThanhId <> "" Then
            '    Dim _row = curTinhThanh.NewRow
            '    _row(0) = selectedTinhThanhId
            '    Dim _curTinhTitle = (From q In Data.DonViHanhChinhs Where q.Id = selectedTinhThanhId Select q.Title).FirstOrDefault
            '    _row(1) = _curTinhTitle
            '    curTinhThanh.Rows.Add(_row)
            'End If

            With rptTinhThanhChinh
                .DataSource = curTinhThanh
                .DataBind()
            End With

        End Using

        '   Session("curTinh") = curTinhThanh



    End Sub

    Protected Sub rptTinhThanhChinh_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptTinhThanhChinh.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim lbtTitle As LinkButton = CType(e.Item.FindControl("lbtTitle"), LinkButton)

  
        lbtTitle.Text = e.Item.DataItem("Title")
        If e.Item.DataItem("Id") = ddlTinhThanh.SelectedValue Then
            lbtTitle.CssClass = "active"
        End If
        lbtTitle.ToolTip = e.Item.DataItem("Id")

    End Sub


    Private Sub BindToGrid(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities

            Dim count As Integer
            Dim a = (From r In Data.uspSelectSubCate(hidCateId.Value) Select r.CateID).ToArray

            ' Dim pro = (From p In data.Products Where (a.Contains(p.CateID) Or p.CateID = strCateId) 

            Dim cat = (From q In Data.Categories Where q.CatId = hidCateId.Value Select q).FirstOrDefault
            '  hplTitle.Text = cat.Title
            '   hplTitle.NavigateUrl = MainDomain & "rao-vat/" & cat.QueryString & "-" & cat.CatId & ".html"

            Dim p As List(Of RaoVat)

            p = (From q In Data.RaoVats Where ((q.TinhTP = _TinhThanhId Or _TinhThanhId = 0)) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending, q.Created Descending Select q).Skip((ipage - 1) * 20).Take(20).ToList

            count = (From q In Data.RaoVats Where (q.TinhTP = _TinhThanhId Or _TinhThanhId = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                            Or q.eEffectiveDate Is Nothing) Select q.RaovatId).Count
            'xem nhieu trong tuan

            lblTotal.Text = count
            lblTotal2.Text = count
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 20, 5)

            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim ltrTinhTrangXe As Literal = CType(e.Item.FindControl("ltrTinhTrangXe"), Literal)


        ltrTinhTrangXe.Text = "<h5>" & getTinhTrangXe(row.RaovatId) & "</h5>"

        hplTitle.Text = "<h4>" & row.Title.Split("(")(0) & "</h4>"
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"
        Dim ltrDescription As Literal = CType(e.Item.FindControl("ltrDescription"), Literal)
        ltrDescription.Text = CropText(row.Description, 100, "...")
        Dim ltrUSD As Literal = CType(e.Item.FindControl("ltrUSD"), Literal)
        If Not row.SellPrice Is Nothing Then
            ltrUSD.Text = "~ " & CType(row.SellPrice.ToString, Double).ToString("#,##0") & " USD"
        End If




        '  hplTitle.NavigateUrl = MainDomain & row.Store.QueryString & "-" & row.StoreId & "/" & row.QueryString & "-" & row.ProductId.ToString & "/chi-tiet.html"



        Dim imgProduct As Image = CType(e.Item.FindControl("imgProduct"), Image)

        imgProduct.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        imgProduct.AlternateText = row.Title

        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        ltrPrice.Text = IIf(row.OriginalPrice = 0, "Call", FormatCurrencyOto(row.OriginalPrice.ToString))

        Dim ltrLienHe As Literal = CType(e.Item.FindControl("ltrLienHe"), Literal)

        Dim ltrTelephone As Literal = CType(e.Item.FindControl("ltrTelephone"), Literal)
        ltrLienHe.Text = row.HoTen & Environment.NewLine & row.DiaChi
        ltrTelephone.Text = row.Phone

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrTelephone.Text = ltrTelephone.Text & " - " & row.PhoneOther
        End If

        Dim ltrMaTin As Literal = CType(e.Item.FindControl("ltrMaTin"), Literal)
        ltrMaTin.Text = "Mã: " & row.Code
        Dim ltrTinhThanh As Literal = CType(e.Item.FindControl("ltrTinhThanh"), Literal)
        If Not IsNothing(row.TinhTP) Then
            ltrTinhThanh.Text = GetTinhThanhPho(row.TinhTP)
        End If


    End Sub
    Private Function getTinhTrangXe(ByVal _RaoVatId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.ThongSoCoBans Where q.RaoVatId = _RaoVatId Select q.TinhTrang, q.NamSanXuat).FirstOrDefault

            If Not p Is Nothing AndAlso p.TinhTrang = 1 Then
                Return " Xe mới " & p.NamSanXuat.ToString
            ElseIf Not p Is Nothing Then
                Return " Xe cũ " & p.NamSanXuat.ToString
            End If
        End Using

    End Function

    Private Function GetTinhThanhPho(ByVal _TinhThanhId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.DonViHanhChinhs Where q.Id = _TinhThanhId Select q.Title).FirstOrDefault
            If Not p Is Nothing Then
                Return p
            End If

        End Using
        Return ""
    End Function

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub


    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub


    Protected Sub ddlTinhThanh_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTinhThanh.SelectedIndexChanged
        BindToGrid(1, ddlTinhThanh.SelectedValue)
        TinhThanhChinh()
    End Sub
    Protected Sub lbtTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle.ToolTip
        BindToGrid(1, lbtTitle.ToolTip)
        TinhThanhChinh()
    End Sub

End Class
